﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cit.CDE.Net.Module.Commons;
using Module;
using Cit.CDE.Net.Module;
using cde.SystemCommon;
using UI.UserControl;
using Cit.CDE.Net.Service.ModuleFactory;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Text;

/// <summary>
/// Copyright (C)   jinggubang
/// FileName:        TrainingViewlist.aspx.cs           
/// Author :         Wangbc
/// CDT:             2013-02-01           
/// Version:         1.0.0.0                
/// Depiction:       培训计划查询   
/// </summary>
public partial class main_sales_TrainingViewlist : UserControl.CommonPage
{
    /// <summary>
    /// EntityManager
    /// </summary>
    private CDEEntityManager em;
    private String EM;
    private string teacher_id;
    private string area_id = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        //当前登陆人 数据权限处理
        LoginOprator op = (LoginOprator)Session[Cit.CDE.Net.Module.Commons.CDEConstants.SESSION_LOIN_OPRATOR];
        //员工只能新增修改自己所属区域数据
        if (op.POST == "0")
        {
            if (op.AREA_ID != "")
            {
                area_id = op.AREA_ID;
            }
        }

        if (!Page.IsPostBack)
        {          
            initPage();
            QueryTraining();
        }
       
    }


    /// <summary>
    /// 查询按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnquery_Click(object sender, EventArgs e)
    {
        //查询
        QueryTraining();

    }

    /// <summary>
    /// 初始化页面数据
    /// </summary>
    private void initPage() {
        //初始开始结束日期
        DateTime stratDate = DateTime.Now;
        this.txt_START_DATE.Text = stratDate.ToString("yyyy/MM/dd");
        this.txt_END_DATE.Text = stratDate.AddDays(7).ToString("yyyy/MM/dd"); 
  
        //初始老师下拉数据
        StringBuilder sqlT = new StringBuilder();
        sqlT.AppendFormat("select ID,NAME from USERS where DEPT_ID={0}", 5);
        if (!string.IsNullOrEmpty(area_id))
        {
            sqlT.AppendFormat(" AND AREA_ID='{0}'", area_id);
        }

        DataTable teacher_ds = Util.loadSql(sqlT.ToString());
        int count_teacher = teacher_ds.Rows.Count;      
        if (count_teacher > 0)
        {
            ddl_TEACHER__NAME.DataSource = teacher_ds.DefaultView;
            //指定DropDownList使用的表里的那些字段
            ddl_TEACHER__NAME.DataTextField = "NAME"; //dropdownlist的Text的字段
            ddl_TEACHER__NAME.DataValueField = "ID";//dropdownlist的Value的字段         
            ddl_TEACHER__NAME.DataBind();
            ddl_TEACHER__NAME.Items.Insert(0,"");
            //ddl_TEACHER__NAME.SelectedIndex = ddl_TEACHER__NAME.Items.Count - 1;
           
        }
         
    }

    private void QueryTraining()
    {      
        DateTime stratDate = DateTime.Parse(this.txt_START_DATE.Text);
        DateTime endDate = DateTime.Parse(this.txt_END_DATE.Text);
        teacher_id = this.ddl_TEACHER__NAME.SelectedValue;
        StringBuilder sqlT = new StringBuilder();
        sqlT.AppendFormat("select ID,NAME from USERS where DEPT_ID={0}",5);
        if (teacher_id != "" && teacher_id != null)
        {
         sqlT.AppendFormat(" and id={0}",teacher_id);
        }

        if (!string.IsNullOrEmpty(area_id))
        {
            sqlT.AppendFormat(" AND AREA_ID='{0}'", area_id);
        }

        DataTable teacher_ds = Util.loadSql(sqlT.ToString());
        int count_teacher = teacher_ds.Rows.Count;
        string[] teachers = new string[count_teacher];
        if (count_teacher > 0)
        {
            for (int i = 0; i < teacher_ds.Rows.Count; i++)
            {
                teachers[i] = Util.objectToString(teacher_ds.Rows[i]["NAME"]);
            }
        }
        else {
            return;
        } 

        int count_day = (endDate - stratDate).Days+1;
        object[,] business_objData = new object[count_teacher+1, count_day+1];

        //初始天数
        business_objData[0, 0] = "培训老师";
        for (int s = 0; s < count_day; s++) {
            business_objData[0, s + 1] = stratDate.AddDays(s).ToString("yyyy/MM/dd");
        }

        //初始老师和状态
        for (int i = 0; i < count_teacher; i++)
        {
            business_objData[i+1, 0] = teachers[i];
            for (int j = 0; j < count_day; j++)
            {
                business_objData[i+1, j+1] = "0";
            }
        }

      


        StringBuilder SQL_CHK = new StringBuilder();
        SQL_CHK.AppendFormat(@"SELECT T.ID AS TID,U.ID AS UID,U.NAME,dbo.DATE_FORMAT(START_DATE,'YYYY/MM/DD') AS START_DATE,dbo.DATE_FORMAT(END_DATE,'YYYY/MM/DD') AS END_DATE,MEMO FROM TRAINING  T,USERS U   WHERE T.TEACHER=U.ID AND
                            ('{0}' BETWEEN START_DATE AND  END_DATE 
                            OR  '{1}' BETWEEN START_DATE AND  END_DATE
                            OR '{0}' = dbo.DATE_FORMAT(START_DATE,'YYYY/MM/DD')
                            OR '{0}' = dbo.DATE_FORMAT(END_DATE,'YYYY/MM/DD')
                            OR '{1}' = dbo.DATE_FORMAT(START_DATE,'YYYY/MM/DD')
                            OR '{1}' = dbo.DATE_FORMAT(END_DATE,'YYYY/MM/DD')
                            OR
                            (
                             '{0}' < dbo.DATE_FORMAT(START_DATE,'YYYY/MM/DD')
                            AND  '{1}' > dbo.DATE_FORMAT(END_DATE,'YYYY/MM/DD')  
                            )                          
                            OR ( '{0}' < dbo.DATE_FORMAT(START_DATE,'YYYY/MM/DD')
                            AND '{1}' > dbo.DATE_FORMAT(END_DATE,'YYYY/MM/DD')
                            )) 
                            ", stratDate.ToString("yyyy/MM/dd"), endDate.ToString("yyyy/MM/dd"));

        if (teacher_id != ""&&teacher_id!=null) {
            SQL_CHK.AppendFormat(" AND TEACHER='{0}'", teacher_id);
        }

        if (!string.IsNullOrEmpty(area_id))
        {
            SQL_CHK.AppendFormat(" AND T.AREA_ID='{0}'", area_id);
        }


        // 查询培训数据
        DataTable traing_ds = Util.loadSql(SQL_CHK.ToString());
         
        Dictionary<string, string> traing_Data = new Dictionary<string, string>();

        int count_traing = traing_ds.Rows.Count; 
        if (count_traing > 0)
        {
            for (int i = 0; i < traing_ds.Rows.Count; i++)
            {
                string UID = Util.objectToString(traing_ds.Rows[i]["UID"]);
                string TID = Util.objectToString(traing_ds.Rows[i]["TID"]);
                string NAME = Util.objectToString(traing_ds.Rows[i]["NAME"]);
                string START_DATE = Util.objectToString(traing_ds.Rows[i]["START_DATE"]);
                string END_DATE = Util.objectToString(traing_ds.Rows[i]["END_DATE"]);
                string MEMO = Util.objectToString(traing_ds.Rows[i]["MEMO"]);

                DateTime t_stratDate = DateTime.Parse(START_DATE);
                DateTime t_endDate = DateTime.Parse(END_DATE);
                int t_count_day = (t_endDate - t_stratDate).Days + 1;
                for (int s = 0; s < t_count_day; s++)
                {
                    string str = NAME + t_stratDate.AddDays(s).ToString("yyyy/MM/dd"); ;
                    if (!traing_Data.ContainsKey(str))
                    {
                        traing_Data.Add(str, MEMO);
                    }  
                } 
            }
        }

        //画面HTML
        for (int i = 1; i < count_teacher + 1; i++)
        {
            string str_teacher = Util.objectToString(business_objData[i,0]);
            for (int j = 1; j < count_day + 1; j++)
            {                
                string str_date = Util.objectToString(business_objData[0, j]);
                string t_date = str_teacher + str_date;
                if (traing_Data.ContainsKey(t_date))
                {
                    business_objData[i, j] = "1";
                }
            }           
        }


        StringBuilder sysmsgStr = new StringBuilder();

        sysmsgStr.AppendFormat("<TABLE border='1' width='800px'>");

        //画面HTML
        for (int i = 0; i < count_teacher+1; i++)
        {
            sysmsgStr.AppendFormat("<TR>");
            for (int j = 0; j < count_day+1; j++)
            {
                string strval = Util.objectToString(business_objData[i, j]);

                if (strval == "1")
                {
                    sysmsgStr.AppendFormat("<TD style='background:yellow'>{0}</TD>", "&nbsp");
                }
                else if (strval == "0")
                {
                    sysmsgStr.AppendFormat("<TD>{0}</TD>", "&nbsp");
                }
                else {
                    sysmsgStr.AppendFormat("<TD>{0}</TD>", strval);
                }

            }
            sysmsgStr.AppendFormat("</TR>");
        }

        sysmsgStr.AppendFormat("</TABLE>");

        sysmsgStr.Append("<div style=\"clear:both;\"></div>");
        courseListDiv.InnerHtml = sysmsgStr.ToString();   
    }
}
